/**
 * Created by pzw on 2015/6/4.
 */

var enthalpy = function() {

    var culF2AndI2 = function() {
        $.get(basePath + "enthalpy/culF2AndI2",function(data) {
            $("#i2_show").html(data.i2);
            $("#f2_show").html(data.f2);
            culB6();
        })
    };

    var culB6 = function() {
        $.get(basePath + "enthalpy/culB6",function(data) {
            $("#B6").val(data);
        })
    };

    return {
        init: function() {
            var b2flag = false;
            var c2flag = false;
            $("#B2").blur(function() {
                var $b2 = $("#B2");
                validate.init($b2);
                if($b2.val()!=""){
                    if(validate.isNotNumber($b2.val())){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "enthalpy/saveB2" ,
                        dataType:"json",
                        type:"post",
                        data:"b2=" + $b2.val(),
                        success:function(data) {
                            b2flag = true;
                            if(data.g2 == 0 && data.h2 == 0) {
                                $("#H2").val("输入错误");
                            } else {
                                $("#g2_show").html(data.g2);
                                $("#h2_show").html(data.h2);
                                $("#H2").val(data.h2);
                            }
                            if (b2flag && c2flag) {
                                culF2AndI2();
                                b2flag = false;
                                c2flag = false;
                            }

                        }
                    })
                }else{
                    validate.invalid();
                    return;
                }
            });

            $("#C2").blur(function() {
                var $c2 = $("#C2");
                validate.init($c2);
                if($c2.val()!=""){
                    if(validate.isNotNumber($c2.val())){
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "enthalpy/saveC2" ,
                        dataType:"json",
                        type:"post",
                        data:"c2=" + $c2.val(),
                        success:function(data) {
                            c2flag = true;
                            if (data.e2 == -1.0) {
                                $("#E2").val("输入错误");
                            }
                            $("#d2_show").html(getValidRecoverVal(data.d2));
                            $("#e2_show").html(data.e2);
                            $("#E2").val(data.e2);
                            if (b2flag && c2flag) {
                                culF2AndI2();
                                b2flag = false;
                                c2flag = false;
                            }
                        }
                    })
                }else{
                    validate.invalid();
                    return;
                }
            })
        },
        recover: function() {
            $.get(basePath + "enthalpy/recover",function(data) {
                $("#B2").val(getValidRecoverVal(data.b2));
                $("#C2").val(getValidRecoverVal(data.c2));
                $("#B6").val(getValidRecoverVal(data.b6));
                $("#d2_show").html(getValidRecoverVal(data.d2));
                $("#e2_show").html(getValidRecoverVal(data.e2));
                $("#g2_show").html(getValidRecoverVal(data.g2));
                $("#h2_show").html(getValidRecoverVal(data.h2));
                $("#H2").val(getValidRecoverVal(data.h2));
                $("#E2").val(getValidRecoverVal(data.e2));
            });
        }
    }

}();
